#include "ClockWidget.h"
#include <QVBoxLayout>
#include <QMessageBox>
#include <QGraphicsDropShadowEffect>
#include <QPropertyAnimation>
ClockWidget::ClockWidget(int radius,QWidget *parent)
    : QWidget(parent),
    m_pChart(nullptr),
    newRadius(radius * 1.5),
    isDragging(false),
    m_closeButton(nullptr),
    opacityEffect(nullptr),
    fadeInAnimation(nullptr),
    fadeOutAnimation(nullptr)
{
    InitData();

    InitForm();
}

ClockWidget::~ClockWidget()
{
    if(m_pChart != nullptr){
        delete m_pChart;
        m_pChart = nullptr;
    }
    if(m_closeButton != nullptr){
        delete m_closeButton;
        m_closeButton = nullptr;
    }
    if(opacityEffect != nullptr){
        delete opacityEffect;
        opacityEffect = nullptr;
    }
    if(fadeInAnimation != nullptr){
        delete fadeInAnimation;
        fadeInAnimation = nullptr;
    }
    if(fadeOutAnimation != nullptr){
        delete fadeOutAnimation;
        fadeOutAnimation = nullptr;
    }
}

void ClockWidget::setRadius(int radius)
{
    if (radius > 0) {
        m_pChart->setRadius(radius);
        newRadius = radius * 1.1;
        update();
    }
}

void ClockWidget::setUpdateInterval(int interval)
{
    if (interval > 0) {
        m_pChart->setUpdateInterval(interval);
        update();
    }
}

void ClockWidget::setTimerMode(ClockChart::TimerMode mode)
{
    m_pChart->setTimerMode(mode);
}

void ClockWidget::setThemeColor(ClockChart::ThemeColor theme)
{
    m_pChart->setThemeColor(theme);
}

void ClockWidget::setInitialTime(int seconds)
{
    if (seconds >= 0) {
        m_pChart->setInitialTime(seconds);
    }
}

void ClockWidget::onClockStatus(bool isPaused)
{
    QString status = isPaused ? "时钟暂停了。" : "时钟启动了";
    QMessageBox::information(this, "时钟状态", status);
}

void ClockWidget::mouseMoveEvent(QMouseEvent *event)
{
    if (isDragging) {
        move(window_top_left_point + (event->globalPos() - mouse_startPoint));
    }
}

void ClockWidget::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        isDragging = true;
        mouse_startPoint = event->globalPos();
        window_top_left_point = frameGeometry().topLeft();
    }
}

void ClockWidget::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        isDragging = false;
    }
}

#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
void ClockWidget::enterEvent(QEnterEvent *event)
#else
void ClockWidget::enterEvent(QEvent *event)
#endif
{
    m_closeButton->setVisible(true);
    fadeInAnimation->start();
    update();
    QWidget::enterEvent(event);
}

void ClockWidget::leaveEvent(QEvent *event)
{
    fadeOutAnimation->start();
    QWidget::leaveEvent(event);
}

void ClockWidget::InitForm()
{
    setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground, true);

    resize(newRadius * 2, newRadius * 2);

    QVBoxLayout* layout = new QVBoxLayout(this);
    layout->addStretch();
    layout->addWidget(m_pChart, 0, Qt::AlignCenter);
    layout->addWidget(m_closeButton, 0, Qt::AlignCenter);
    layout->addStretch();

    setLayout(layout);
}


void ClockWidget::InitData()
{
    m_pChart = new ClockChart(this);
    m_pChart->setRadius(40);
    m_pChart->setInitialTime(100);
    connect(m_pChart, &ClockChart::sigClockStatus, this, &ClockWidget::onClockStatus);

    m_closeButton = new QPushButton(this);
    m_closeButton->setIcon(QIcon(":/resource/svg/close_black.svg"));
    m_closeButton->setIconSize(QSize(newRadius/6, newRadius/6));
    m_closeButton->resize(newRadius/6, newRadius/6);
    m_closeButton->setStyleSheet("QPushButton { border: none; padding: 0px; margin: 0px; }");
    m_closeButton->setVisible(false);
    connect(m_closeButton, &QPushButton::clicked, this, &ClockWidget::close);

    opacityEffect = new QGraphicsOpacityEffect(this);
    m_closeButton->setGraphicsEffect(opacityEffect);

    fadeInAnimation = new QPropertyAnimation(opacityEffect, "opacity");
    fadeInAnimation->setDuration(500);
    fadeInAnimation->setStartValue(0.0);
    fadeInAnimation->setEndValue(1.0);

    fadeOutAnimation = new QPropertyAnimation(opacityEffect, "opacity");
    fadeOutAnimation->setDuration(500);
    fadeOutAnimation->setStartValue(1.0);
    fadeOutAnimation->setEndValue(0.0);

    // 创建阴影效果
    QGraphicsDropShadowEffect *shadowEffect = new QGraphicsDropShadowEffect(this);
    shadowEffect->setBlurRadius(10);
    shadowEffect->setColor(QColor(33, 33, 33, 128));
    shadowEffect->setOffset(3, 3);
    m_pChart->setGraphicsEffect(shadowEffect);
}

